import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        test4();
    }
    public static void test1() {
        MySingleLinkedList mySingleLinkedList = new MySingleLinkedList();
        mySingleLinkedList.creatList();
        mySingleLinkedList.display();
        System.out.println("-------------");
        System.out.println(mySingleLinkedList.size());
        System.out.println("-------------");
        System.out.println(mySingleLinkedList.contains(40));
        System.out.println("-------------");
        mySingleLinkedList.addFirst(1);
        mySingleLinkedList.addFirst(999);
        mySingleLinkedList.display();
        System.out.println("-------------");
        mySingleLinkedList.clear();
        mySingleLinkedList.addLast(10);
        mySingleLinkedList.addFirst(100);
        mySingleLinkedList.addFirst(1000);
        mySingleLinkedList.addLast(9);
        mySingleLinkedList.display();
        System.out.println("-------------");
        mySingleLinkedList.addIndex(5,0);
        mySingleLinkedList.display();
        System.out.println("-------------");
    }
    public static void test2() {
        MySingleLinkedList mySingleLinkedList = new MySingleLinkedList();
        mySingleLinkedList.addFirst(1000);
        mySingleLinkedList.addFirst(100);
        mySingleLinkedList.addFirst(10);
        mySingleLinkedList.addFirst(10);
        mySingleLinkedList.addFirst(1);
        mySingleLinkedList.addFirst(0);
        mySingleLinkedList.addFirst(0);
        mySingleLinkedList.display();
        mySingleLinkedList.remove(0);
        mySingleLinkedList.display();
    }
    public static void test3() {
        MySingleLinkedList mySingleLinkedList = new MySingleLinkedList();
        mySingleLinkedList.addFirst(56);
        mySingleLinkedList.addFirst(12);
        mySingleLinkedList.addFirst(23);
        mySingleLinkedList.addFirst(12);
        mySingleLinkedList.addFirst(23);
        mySingleLinkedList.addFirst(23);
        mySingleLinkedList.display();
        mySingleLinkedList.removeAllKey(23);
        mySingleLinkedList.display();
        mySingleLinkedList.clear();
        mySingleLinkedList.display();
    }
    public static void test4() {
        MySingleLinkedList mySingleLinkedList = new MySingleLinkedList();
        mySingleLinkedList.addFirst(56);
        mySingleLinkedList.addFirst(12);
        mySingleLinkedList.addFirst(1000);
        mySingleLinkedList.addFirst(12);
        mySingleLinkedList.addFirst(23);
        mySingleLinkedList.addFirst(23);
        mySingleLinkedList.display();
        /*int value = mySingleLinkedList.kthToLast(3);
        System.out.println(value);*/
        MySingleLinkedList.ListNode partition = mySingleLinkedList.partition(22);
        mySingleLinkedList.display(partition);

    }
    public MySingleLinkedList.ListNode mergeTwoLists(MySingleLinkedList.ListNode headA,
                                                     MySingleLinkedList.ListNode headB) {
        MySingleLinkedList.ListNode headNew = new MySingleLinkedList.ListNode(-1);
        MySingleLinkedList.ListNode temp = headNew;
        while (headA != null && headB != null) {//当有一个链表没有节点了，此时就可能结束循环了
            if (headA.value >= headB.value) {
                temp.next = headB;
                temp = headB;
                headB = headB.next;
            } else {
                temp.next = headA;
                temp = headA;
                headA = headA.next;
            }
        }
        if (headB == null) {
            temp.next = headA;
        }
        if (headA == null) {
            temp.next = headB;
        }
        return headNew.next;
    }
    public MySingleLinkedList.ListNode getIntersectionNode(MySingleLinkedList.ListNode headA, MySingleLinkedList.ListNode headB) {
        int lenA = 0;
        int lenB = 0;
        MySingleLinkedList.ListNode pA = headA;
        MySingleLinkedList.ListNode pB = headB;
        //计算lenA的值
        while (pA != null) {
            lenA++;
            pA = pA.next;
        }
        //计算lenB的值
        while (pB != null) {
            lenB++;
            pB = pB.next;
        }
        int len = lenA - lenB;
        pA = headA;
        pB = headB;
        if(len < 0) {
            pA = headB;
            pB = headA;
            len = lenB - lenA;
        }
        //让长的pA指向的链表移动len步
        while (len != 0) {
            pA = pA.next;
            len--;
        }
        while (pA != pB) {
            pA = pA.next;
            pB = pB.next;
        }
        return pA;
    }
}